package net.web.milkteapos.module;

import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.web.milkteapos.bean.Product;
import net.web.milkteapos.bean.Sale;
import net.web.milkteapos.bean.SaleList;

import org.apache.catalina.startup.SetAllPropertiesRule;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/report")
@Ok("json")
@Fail("http:500")
public class ReportModule {

	@Inject
	protected Dao dao;
	
	@At("/monthSales")
	public Object monthSales(@Param("year") int year) {
		List<Integer> sales = new ArrayList<>();
		Map<Object, Integer> map = new HashMap<>();

		//int year = 2017;
		Cnd cnd;
		for(int i = 1;i<13;i++){
			if(i<10){
				 cnd = Cnd.where("orderDate", "like", "%"+year+"-0"+i+"%");
			}	
			else{
				 cnd = Cnd.where("orderDate", "like", "%"+year+"-"+i+"%");
			}
			int amounts = 0;
	        List<Sale> result = dao.query(Sale.class, cnd);   
	        for (Sale model : result) {
				dao.fetchLinks(model, "totalAmount");
				amounts += model.getTotalAmount().getAmount();
				System.out.println("输出:"+model.getTotalAmount().getAmount());
				
			}
	        System.out.println(year+"年"+i+"月销售总额:" + amounts);
	      sales.add(amounts);
	       
		}
		NutMap re = new NutMap();
        re.setv("ok", true).setv("list", sales);
        re.setv("ok", true).setv("map", map);
        return re;
        
	}
	
	
	//一个时间段每种产品的销量
	@At("/saleNum")
	public Object queryAll(@Param("date1") String date1, @Param("date2") String date2) {
		Cnd cnd = null;
        List<Object> sales = new ArrayList<>();
//        Cnd cnd = Cnd.where("orderDate", ">" , "2017-04-30").and("orderDate", "<" , "2017-06-03");
        if(date1!=null&&date2!=null){
        	 cnd = Cnd.where("orderDate", ">" , date1).and("orderDate", "<" , date2);
        }
        List<Sale> result = dao.query(Sale.class, cnd);
        List<Product> result1 = dao.query(Product.class, null);
        
        Map<Object, Integer> map = new HashMap<>();
        for(Product m : result1){
        	dao.fetchLinks(m, "productName");
        	dao.fetchLinks(m, "productId");
        	String name = m.getProductName();
        	
        	System.out.println(m.getProductName());
        	
	        int sum = 0;
	        for (Sale model : result) {
				dao.fetchLinks(model, "products");
				int sum1 = 0;
				for (Product product : model.getProducts()) {
					dao.fetchLinks(product, "money");
					
					sum1 = dao.query(SaleList.class, Cnd
							   .where("orderId", "=", model.getOrderId())
							   .and("productId", "=", m.getProductId())).size();
					System.out.println(
							"1414    "+sum1);
				}
				sum += sum1;
				System.out.println("总量：" + sum);
				
				
			}
	        sales.add(name);
	        sales.add(sum);
        }
		NutMap re = new NutMap();
        re.setv("ok", true).setv("list", sales);
        re.setv("ok", true).setv("map", map);
        return re;
	}
	
	
}
